package com.leyou.common.advice;

import com.leyou.common.exception.LyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice//异常处理器
@Slf4j
public class ControllerExceptionAdvice {

    /*
    * todo
    *  统一异常的处理方法，@ExceptionHandler(RuntimeException.class)
    *   声明这个方法是处理RuntimeException这样异常的。
    *  @param ex 捕获到的异常
    *   @return 返回给页面的状态码和信息
    *
    * */
    //@ExceptionHandler(RuntimeException.class)
    @ExceptionHandler(LyException.class)
    public ResponseEntity<String> handleLyException(LyException ex){
        //return ResponseEntity.status(400).body(ex.getMessage());
        return ResponseEntity.status(ex.getStatus()).body(ex.getMessage());
    }

}
